package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.LawEnforcementDO;
import com.triones.ljr.entity.vo.LawEnforcementVO;
import com.triones.ljr.mapper.LawEnforcementMapper;
import com.triones.ljr.service.LawEnforcementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 执法事项
 */
@Service
@RequiredArgsConstructor
public class LawEnforcementServiceImpl implements LawEnforcementService {

    private final LawEnforcementMapper lawEnforcementMapper;

    @Override
    public TableDataInfo<LawEnforcementVO> pageList(LawEnforcementDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<LawEnforcementDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(LawEnforcementDO::getIsDel,"0")
            .like(StringUtils.isNotBlank(dto.getTitle()), LawEnforcementDO::getTitle, dto.getTitle())
            .orderByAsc(LawEnforcementDO::getSort);
        Page<LawEnforcementVO> result = lawEnforcementMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean add(LawEnforcementDO dto) {
        dto.setCreateTime(DateUtil.getDateTimeString());
        return lawEnforcementMapper.insert(dto) > 0;
    }

    @Override
    public boolean edit(LawEnforcementDO dto) {
        return lawEnforcementMapper.updateById(dto) > 0;
    }

    @Override
    public LawEnforcementVO getInfo(Long id) {
        return lawEnforcementMapper.selectVoById(id);
    }

    @Override
    public boolean remove(Long id) {
        LawEnforcementDO enforcementDO = lawEnforcementMapper.selectById(id);
        if (enforcementDO == null) {
            throw new ServiceException("信息不存在");
        }
        enforcementDO.setIsDel("1");
        return lawEnforcementMapper.updateById(enforcementDO) > 0;
    }

    @Override
    public List<LawEnforcementVO> list(String category) {
        return lawEnforcementMapper.selectVoList(new LambdaQueryWrapper<LawEnforcementDO>()
            .eq(LawEnforcementDO::getIsDel,"0")
            .eq(ObjectUtil.isNotEmpty(category),LawEnforcementDO::getCategory,category)
            .orderByAsc(LawEnforcementDO::getSort));
    }
}
